import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @ClassName: Demo01
 * @Description: TODO JDBC 大概六个步骤
 * @Author: MaykkStar@163.com
 */
public class Demo01 {
    public static void main(String[] args) throws Exception {
        // 注册驱动
        // ——将MySQL的驱动包导入项目
        // 通过代码注册驱动
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
        Class.forName("com.mysql.jdbc.Driver");

        // 建立连接
        final String URL = "jdbc:mysql://localhost:3306/maykk?useSSL=false";
        final String USER = "root";
        final String PWD = "123456";
        Connection conn = DriverManager.getConnection(URL, USER, PWD);
        // 创建执行 SQL 语句的对象
        Statement st = conn.createStatement();
        // 组织并运行 SQL 语句
        String sql = "select `id`,`name`,`sex`,`age`,`score`,`tel` from `stu`";
        ResultSet rs = st.executeQuery(sql);
        // 处理结果
            System.out.println("id\tname\tsex\tage\tscore\ttel");
        while (rs.next()) {
            System.out.printf(
                    // rs.getInt(1) + "\t" +
                    // rs.getString(2) + "\t" +
                    // rs.getObject(3) + "\t" +
                    // rs.getInt(4) + "\t" +
                    // rs.getFloat(5) + "\t" +
                    // rs.getString(6) + "\t"

                    "├————————————————————————————————————————————┥\n"+
                    "|%d\t|%3s\t|%s\t|%d\t|%4.1f\t|%s|\n"+
                    "├————————————————————————————————————————————┥\n",
                    // rs.getInt(1),
                    // rs.getString(2),
                    // rs.getObject(3),
                    // rs.getInt(4),
                    // rs.getFloat(5),
                    // rs.getString(6)
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getObject("sex"),
                    rs.getInt("age"),
                    rs.getFloat("score"),
                    rs.getString("tel")
            );
        }
        // 释放资源
        rs.close();
        st.close();
        conn.close();
    }
}
